pub mod lebocast;
pub mod net;

use std::io::Write;

pub fn setup_log() {
    let mut builder = env_logger::Builder::new();
    let env = env_logger::Env::default().default_filter_or("info");
    // builder.parse_filters("libmdns=debug");
    builder.parse_env(env);
    builder.format(|buf, record| {
        let mut style = buf.style();
        style.set_bold(true);
        match record.level() {
            log::Level::Error => {
                style.set_color(env_logger::fmt::Color::Red);
            }
            log::Level::Warn => {
                style.set_color(env_logger::fmt::Color::Yellow);
            }
            log::Level::Info => {
                style.set_color(env_logger::fmt::Color::Green);
            }
            _ => (), // log::Level::Debug => todo!(),
                     // log::Level::Trace => todo!(),
        };
        writeln!(
            buf,
            "[{} {} {}:{}] {}",
            chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
            style.value(record.level()),
            record.module_path().unwrap_or("unknown"),
            record.line().unwrap_or(0),
            record.args()
        )
    });
    builder.init();
}

pub fn hex_to_buf(hex_str: &str) -> Vec<u8> {
    let count = hex_str.chars().count();
    let mut buf = Vec::with_capacity(count / 2);
    for i in 0..(count / 2) {
        let i = i * 2;
        let q = &hex_str[i..i + 2];
        buf.push(u8::from_str_radix(q, 16).unwrap_or(0));
    }
    buf
}
